<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller {
	/**
	 * 首页主体
	 * @return [type] [description]
	 */
    public function index(){
    	$hotArticleList = $this->hotArticle(); //热门推荐
    	$clickArticleList = $this->clickArticle();  //点击排行
    	$moreArticleList = $this->moreArticle(); //最新文章
    	$this->assign('hotArticleList',$hotArticleList);
    	$this->assign('clickArticleList',$clickArticleList);
    	$this->assign('moreArticleList',$moreArticleList);
        $this->display('index');
    }
	
	/**
	 * 查询热门文章
	 * @return array 热门文章
	 */
	public function hotArticle(){
		$hotArticleList = D('Article')->sortArticleSelect(['article_id','article_title','num_comment'],'num_comment desc');
		return $hotArticleList;
	}

	/**
	 * 查询点击排行文章
	 * @return array 点击排行文章
	 */
	public function clickArticle(){
		$clickArticleList = D('Article')->sortArticleSelect(['article_id','article_title','click'],'click desc');
		return $clickArticleList;
	}

	/**
	 * 显示更多文章
	 * @return array 加载出的文章
	 */
	public function moreArticle(){
		$fieldString = "article_id,article_title,article_img,article_digest,article_time,click,is_display,num_comment";
		$endPage = C('MORE_PAGE_LIMIT');
		if (IS_AJAX) {
			$page = I('post.page');
			//计算页码偏移量
			$startPage = $page*$endPage-$endPage;
			$data = D('Article')->moreArticleSelect($fieldString,$startPage,$endPage);
			$this->ajaxReturn($data);
		} else {
			//首页默认显示文章
			return D('Article')->moreArticleSelect($fieldString,$endPage);
		}
	}


}